﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ConfigurationTool
{
    class Configuration
    {
        private String outputFileName = "settings.ini";
        private String outputFilePath = "./";
        private Dictionary<String, Dictionary<String, Control>> sections;
        private const String version = "1.0";

        public Configuration()
        {}

        public Configuration(String iFileName)
        {
            outputFileName = iFileName;
        }

        public Configuration(String iFileName, String iFilePath)
        {
            outputFileName = iFileName;
            outputFilePath = iFilePath;
        }

        public String getVersion()
        {
            return version;
        }

        public Dictionary<String, Dictionary<String, Control>> getSections()
        {
            return sections;
        }

        public void bind( String section, String key, Control control)
        {
            if (!sections.ContainsKey(section))
                sections.Add(section, new Dictionary<String, Control>());
            if (!sections[section].ContainsKey(key))
                sections[section].Add(key, control);
            else if (!sections[section].ContainsValue(control))
                sections[section][key] = control;
        }

        public bool write()
        {
            StreamWriter sw = new StreamWriter(outputFilePath + outputFileName);
            String content = this.getFormContent();
            sw.Write(content);
            sw.Close();

            return true;
        }

        public bool write(String fileName)
        {
            StreamWriter sw = new StreamWriter(fileName);
            String content = this.getFormContent();
            sw.Write(content);
            sw.Close();

            return true;
        }

        public void writeDefault()
        {
            StreamWriter sw = new StreamWriter(outputFilePath + outputFileName);
            String content = getDefaultFile();
            sw.Write(content);
            sw.Close(); 
        }

        public String getFormContent()
        {
            String s = "; BlindBob configuration file version " + this.getVersion() + "\r\n";
            foreach (var key in sections.Keys)
            {
                s += "\r\n[" + key + "]\r\n";

                foreach (var value in sections[key])
                {
                    s += value.Key + "=" + value.Value.Text + "\r\n";
                }
            }
            return s;
        }

        private void SetSection(String section, String[] keyValue)
        {
            if (sections.ContainsKey(section))
            {
                sections[section].Add(keyValue[0], new Control(keyValue[1]));
            }
            else
            {
                Dictionary<String, Control> d = new Dictionary<String, Control>();
                d.Add(keyValue[0], new Control(keyValue[1]));
                sections.Add(section, d);
            }
        }

        public void read()
        {
            ResetAll();

            StreamReader reader = new StreamReader(outputFilePath + outputFileName);
            
            String tmpStr = " ";

            while (tmpStr != null)
            {
                if (tmpStr != null && tmpStr.Contains("["))
                {
                    // crea section
                    String section = tmpStr.Substring(1, tmpStr.Length - 2);
                    tmpStr = section;

                    while (tmpStr != null && !tmpStr.Contains("["))
                    {
                        tmpStr = reader.ReadLine();

                        if (tmpStr != null && !tmpStr.Contains(";") && !tmpStr.Equals("") && !tmpStr.Contains("["))
                        {
                            String[] keyValue = tmpStr.Split('=');

                            SetSection(section, keyValue);
                        }
                    }
                }
                else
                {
                    tmpStr = reader.ReadLine();
                }
            }

            reader.Close();
        }

        public void read(String fileName)
        {
            ResetAll();

            StreamReader reader = new StreamReader(fileName);

            String tmpStr = " ";

            while (tmpStr != null)
            {
                if (tmpStr != null && tmpStr.Contains("["))
                {
                    // crea section
                    String section = tmpStr.Substring(1, tmpStr.Length - 2);
                    tmpStr = section;

                    while (tmpStr != null && !tmpStr.Contains("["))
                    {
                        tmpStr = reader.ReadLine();

                        if (tmpStr != null && !tmpStr.Contains(";") && !tmpStr.Equals("") && !tmpStr.Contains("["))
                        {
                            String[] keyValue = tmpStr.Split('=');

                            SetSection(section, keyValue);
                        }
                    }
                }
                else
                {
                    tmpStr = reader.ReadLine();
                }
            }

            reader.Close();
        }

        private void ResetAll()
        {
            sections = null;
            sections = new Dictionary<String, Dictionary<String, Control>>();
        }

        private String getDefaultFile()
        {
            String file = "";

            file += "; BlindBob configuration file version " + getVersion() + "\r\n";
            file += "\r\n[Keyboard]\r\n";
            file += "Up=W\r\n";
            file += "Down=S\r\n";
            file += "Right=D\r\n";
            file += "Left=A\r\n";
            file += "Jump=Space\r\n";
            file += "Shoot=N\r\n";
            file += "Club=C\r\n";
            file += "Score=L\r\n";
            file += "Pouse=P\r\n";
            file += "Music On/Off=M\r\n";
            file += "Sound On/Off=O\r\n";
            file += "Help=H\r\n";
            file += "Screenshot=T\r\n";
            file += "FPS=F\r\n";
            file += "\r\n[Gamepad]\r\n";
            file += "LTrigger=Screenshot\r\n";
            file += "LShoulder=Sound On/Off\r\n";
            file += "Up=Up\r\n";
            file += "Left=Left\r\n";
            file += "Right=Right\r\n";
            file += "Bottom=Down\r\n";
            file += "RTrigger=Music On/Off\r\n";
            file += "RShoulder=Help\r\n";
            file += "Y=Club\r\n";
            file += "X=Jump\r\n";
            file += "B=FPS\r\n";
            file += "A=Shoot\r\n";
            file += "\r\n[Wiimote]\r\n";
            file += "Up=Up\r\n";
            file += "Left=Left\r\n";
            file += "Right=Right\r\n";
            file += "Bottom=Down\r\n";
            file += "-=Score\r\n";
            file += "+=Help\r\n";
            file += "Home=Pouse\r\n";
            file += "A=Jump\r\n";
            file += "B=Shoot\r\n";
            file += "1=Music On/Off\r\n";
            file += "2=Sound On/Off\r\n";
            file += "\r\n[Other Settings]\r\n";
            file += "Graphics=1024x768\r\n";
            file += "VSync=False\r\n";
            file += "Difficulty=Hard\r\n";

            return file;
        }
    }
}
